/*
 * @lc app=leetcode.cn id=1351 lang=typescript
 *
 * [1351] 统计有序矩阵中的负数
 */

// @lc code=start
function countNegatives(grid: number[][]): number {
    let count = 0;
    for (let i = grid.length - 1; i >= 0; i--) {
        const row = grid[i];
        if (row[0] < 0) {
            count += row.length;
            continue;
        }
        if (row[row.length - 1] >= 0) break;
        for (let j = row.length - 1; j >= 0; j--) {
            if (row[j] < 0) {
                count++;
            } else {
                break;
            }
        }
    }
    return count;
}
// @lc code=end
